﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Resources;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Collections.Generic;

using System.Reflection;

namespace SilverlightApplication1
{
  public class Class1
  {



    public static void loadXap(string Address, UriKind u)
    {
      //http://weblogs.asp.net/razan/archive/2010/01/14/emulating-synchronous-web-service-call-in-silverlight.aspx
      //AppViewportControl.Children.
      //UserControl currentPage = PageContainer.Children[0] as UserControl;
      //if (currentPage != page3) PageContainer.Children[0] = page3;

      WebClient client = new WebClient();
      //client.
      client.OpenReadCompleted += new OpenReadCompletedEventHandler(xapLoad_OpenReadCompleted);
      client.OpenReadAsync(new Uri(Address, u));

    }



    private static void xapLoad_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {

      WebClient b = (WebClient)sender;
      MainPage r = (MainPage)sender;
      


      if (e.Error != null)
        throw new NotImplementedException();

      StreamResourceInfo sri = new StreamResourceInfo(e.Result, null);
      StreamResourceInfo sri4dll = Application.GetResourceStream(sri,
                                        new Uri("SilverlightApplication1.dll", UriKind.Relative));
      AssemblyPart asmPart = new AssemblyPart();
      Assembly asm = asmPart.Load(sri4dll.Stream);
      UserControl uc = asm.CreateInstance("SilverlightApplication1.MainPage") as UserControl;
      //page3 = uc;
      //PageContainer.Children[0] = page3;
      //stBar.Text = "Page3 loaded from server at " + DateTime.Now.ToLongTimeString();
    }


  }
}
